package com.das.mechanic_base.adapter.main;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.DetectionRankBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3RankAdapter extends RecyclerView.Adapter<X3RankAdapter.RankHolder> {

    private Context mContext;

    private int index;

    private List<DetectionRankBean.VoListBean.ListBean> mList;

    public X3RankAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeCurrentIndex(int index) {
        this.index = index;
        notifyDataSetChanged();
    }

    public void changeAdapter(List<DetectionRankBean.VoListBean.ListBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void addAdapter(List<DetectionRankBean.VoListBean.ListBean> mList) {
        this.mList.addAll(mList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RankHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RankHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_rank_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RankHolder holder, int position) {

        holder.tv_grade.setText("NO." + mList.get(position).rank);

        X3GlideNewUtils.loadCircleImage(mContext, X3StringUtils.getImageUrl(mList.get(position).staffBaseImgUrl),holder.iv_staff,R.mipmap.x3_car_owner_header);

        holder.tv_name.setText(mList.get(position).staffBaseName + "");

        long workBaseAmount = mList.get(position).workBaseAmount;
        if (workBaseAmount <= 1) {
            holder.tv_num.setText(String.format(mContext.getString(R.string.alone_num_staff_one),mList.get(position).workBaseAmount));
        }else {
            holder.tv_num.setText(String.format(mContext.getString(R.string.alone_num_staff), mList.get(position).workBaseAmount));
        }

        String country = X3StringUtils.isEmpty(mList.get(position).country) ? "" : mList.get(position).country;

        holder.tv_country.setVisibility((index == 0) ? View.GONE : View.VISIBLE);

        holder.tv_country.setText( country + "");

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class RankHolder extends RecyclerView.ViewHolder {
        TextView tv_grade;
        ImageView iv_staff;
        TextView tv_name;
        TextView tv_num;
        TextView tv_country;
        public RankHolder(@NonNull View itemView) {
            super(itemView);
            tv_grade = itemView.findViewById(R.id.tv_grade);
            iv_staff = itemView.findViewById(R.id.iv_staff);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_country = itemView.findViewById(R.id.tv_country);
        }
    }
}
